package com.selen.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.selen.dto.PageDTO;
import com.selen.dto.PermissionInsertDTO;
import com.selen.dto.PermissionUpdateDTO;
import com.selen.entities.Permission;
import com.selen.exception.ServiceException;
import com.selen.mapper.PermissionMapper;
import com.selen.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Selen
 */
@Service
@CacheConfig(cacheNames = "permission")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @CacheEvict(allEntries = true)
    @Override
    public int insert(PermissionInsertDTO dto) {
        Permission permission = BeanUtil.copyProperties(dto, Permission.class);
        // 设置默认值
        permission.setDeleted(0);
        permission.setCreated(LocalDateTime.now());
        permission.setUpdated(LocalDateTime.now());
        // DB添加
        int result = permissionMapper.insert(permission);
        if (result <= 0) {
            throw new ServiceException("DB添加失败");
        }
        return result;
    }

    @Cacheable(key = "#id", condition = "#id != null", unless = "#result == null")
    @Override
    public Permission selectById(Long id) {
        Permission result = permissionMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int updateById(PermissionUpdateDTO dto) {
        Permission permission = BeanUtil.copyProperties(dto, Permission.class);
        // 设置默认值
        permission.setCreated(null);
        permission.setUpdated(LocalDateTime.now());
        // DB修改
        int result = permissionMapper.updateById(permission);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        // todo 查询该记录是否正在被引用
        int result = permissionMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB逻辑删除失败");
        }
        return result;
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.list();
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        // todo 查询该记录是否正在被引用
        int result = permissionMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批删失败");
        }
        return result;
    }

    @Cacheable(key = "#root.methodName + ':' + " +
            "#dto.getPageNum() + ':' + " +
            "#dto.getPageSize()", unless = "#result == null ")
    @Override
    public PageInfo<Permission> page(PageDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return new PageInfo<>(permissionMapper.list());
    }

}
